package org.grow.mvcpractise.controller;

import org.grow.mvcpractise.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PersonController {

//    http://localhost:9090/personForm post
//    name=zhangsan&age=19&male=true urlencoding
//    json

    @PostMapping("/personForm")
    @ResponseBody
    public Person add(@RequestParam String name,
                      @RequestParam int age,
                      @RequestParam boolean male){

        Person person = new Person();
        person.setName(name);
        person.setAge(age);
        person.setMale(male);

        System.out.println("后端接收到：　"+person);


        return person;

    }


//    http://localhost:9090/personAjax post
//    json json

    @PostMapping("/personAjax")
    @ResponseBody
    public Person insert(@RequestBody Person person){

        System.out.println("后端接收到一个Json转化的　"+person);
        person.setAge(100);


        return person;

    }

    @GetMapping("/personGet")
    @ResponseBody
    public Person get(HttpServletRequest request){

        Person person = new Person();
        String password = (String) request.getAttribute("password");
//        request.getAttribute()
        if (password!=null){
            System.out.println("当前是有密码的　密码是：　"+password);
            person.setAge(99);
        }else {

            person.setAge(66);
        }
        person.setMale(false);
        person.setName("lisi");
        return person;
    }



}
